$(function(){
	
	//判断选择的分类是否是顶级分类及获取类型属性数据
	$('select[name=cid]').change(function(){
		$('.attr').empty();
		$('.spec').empty();
		var cid = parseInt($(':selected' ,this).attr('pid'));
		if(cid == 0){
			$('.ermsg').html("<font color='red'>&nbsp&nbsp*顶级分类不能添加商品！</font>");
			return false;
		}else{
			$('.ermsg').html("");
		}
		var tid = $(':selected',this).attr('tid');
		$("input[name=tid]").val(tid);
		//异步提取类型属性及规格
		$.post(getAttrUrl , {'tid':tid} ,function(data){
			if(data.ermsg == '0'){
				return false;
			}
			//拆分每个对象进行处理
			var attr = '';//属性节点字符串
			var spec = '';//规格节点字符串
			attr += '<tr><td width="75"><font color="#6EA7D1" size="3"><b>属性</b></font></td><td colspan="3"></td></tr>';
			spec += '<tr><td width="75"><font color="#6EA7D1" size="3"><b>规格</b></font></td><td colspan="3"></td></tr>';
			$.each(data , function(i , k){
				if(k.type == 1 ){ //添加规格节点
					spec += '<tr><td>'+k.name+'</td>';
					spec += '<td class="w200"><select name="spec['+k.id+'][value][]">';
					spec += '<option value="">--请选择--</option>';
					var option = k.value.split(',');
					for(var i=0; i < option.length; i++){
						spec += '<option value="'+option[i]+'">'+option[i]+'</option>';
					}
					spec += '</select></td>';
					spec += '<td class="w200">价格：<input type="text" name="spec['+k.id+'][price][]" class="w100" /></td>';
					spec += '<td><span class="addSpec"><font color="green">[+]添加一项</font></span></td></tr>';
					$('.spec').html(spec);
				}else if(k.type ==0){//添加属性节点
					attr += '<tr><td>'+k.name+'</td>';
					if(k.value == ''){						
						attr +='<td><input name="attr['+k.id+']" type="text" class="w100" /></td>';
					}else{
						attr += '<td><select name="attr['+k.id+']">';
						attr += '<option value="">--请选择--</option>';
						var option = k.value.split(',');
						for(var i=0; i < option.length; i++){
							attr += '<option value="'+option[i]+'">'+option[i]+'</option>';
						}
						attr += '</select></td></tr>';
					}
					$('.attr').html(attr);
				}
			});
		},'json');
	});

	//添加一个规格
	$('.addSpec').live('click' , function(){
		var obj =  $(this).parents('tr');
		var newObj = obj.clone();
		//添加项始终在最后
		// newObj.insertAfter(obj);
		// $(this).parent().html("<span class='delSpec'><font color='red'>[-]删除规格</font></span>");

		//添加项始终在最前
		var index = obj.index();
		var addChild = $('.spec tr[sign='+index+']');
		var delMsg = '<span class="delSpec" style="color:red">[-]删除规格</span>';
		newObj.attr('sign',index).find('td:eq(3)').empty().html(delMsg);
		if(addChild.length > 0){
			addChild.eq(addChild.length - 1).after(newObj);
		}else{
			obj.after(newObj);
		}
	});

	//删除一个规格
	$('.delSpec').live('click' , function(){
		var obj = $(this).parents('tr');
		obj.remove();
	})
	

	//点击返回商品类型列表
	$("input[name='baclist']").click(function(){
		location.href=goodsListPath;
	});

	//上传商品列表图
	$("#listimg").uploadify({
        height        : 28,
        buttonImage   : buttonImage,
        swf           : swf,
        uploader      : listImgUploader,
        formData : { 'session_id' : session_id},
        width         : 98,
        uploadLimit	  : 1,
        onUploadSuccess : function(file, data, response){
            eval("var data = " + data);
            if (data.status) {
                var path = listImgPath + data.path;
                $('#listimgshow').css('display','block');
                $('#listimgshow img').attr('src', path);
                $('input[name=listpic]').val(path);
            } else {
                alert(data.msg);
            }
        } 
    });

    //上传商品图册
	$("#goodsimg").uploadify({
        height        : 28,
        buttonImage   : buttonImage,
        swf           : swf,
        uploader      : goodsImgUploader,
        formData : { 'session_id' : session_id},
        width         : 98,
        uploadLimit : 5,
        onUploadSuccess : function(file, data, response){
            eval("var data = " + data);
            if (data.status) {
                var max = goodsImgPath + data.path.savename;
                var medium = goodsImgPath + data.path.medium;
                var mini = goodsImgPath + data.path.mini;
                $('#goodsimgshow').css('display','block');
                var img = '<img src="'+max+'" width="150" height="100">';
                var input = '<input type="hidden" name="max[]" value="'+max+'">';
                input += '<input type="hidden" name="medium[]" value="'+medium+'">';
                input += '<input type="hidden" name="mini[]" value="'+mini+'">';
                $('#goodsimgshow').append(img);
                $('#goodsimgspath').append(input);
            } else {
                alert(data.msg);
            }
        } 
    });

	//展开商品介绍信息编辑框
    $('.addgoodsintroducebtn').click(function(){
    	$(this).empty().html("");
    	$('.giueditshow').css('display','block');
    });

    //展开商品售后服务信息编辑框
    $('.addaftersalebtn').click(function(){
    	$(this).empty().html("");
    	$('.asueditshow').css('display','block');
    });
});